package com.zizo.autohome.emotion.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.hankcs.hanlp.HanLP;
import com.hankcs.hanlp.seg.common.Term;
import com.zizo.autohome.emotion.bean.Topic;
import com.zizo.util.ConnectionPool;

public class TopicUtil {
	/**
	 * @Title: readFile2List
	 * @Description: 读取文件的内容，返回每行内容的List集合
	 * @param pathname
	 * @return
	 * @return List<String>
	 * @author Alexis
	 */
	public final List<String> readFile2List(String pathname) {
		List<String> contentList = new ArrayList<String>();
		BufferedReader br = null;
		FileReader fr = null;
		File file = new File(pathname);
		try {
			fr = new FileReader(file);
			br = new BufferedReader(fr);
			String temp = "";
			while ((temp = br.readLine()) != null) {
				contentList.add(temp);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				fr.close();
				br.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return contentList;
	}

	public static final List<Term> getTermList(String content) {
		List<Term> termList = new ArrayList<Term>();
		termList = HanLP.segment(content);
		return termList;
	}

	/**
	 * @Title: getTopic
	 * @Description: 读Topic
	 * @param path
	 * @return
	 * @return List<Topic>
	 * @author Alexis
	 */
	public List<Topic> getTopic(String path) {
		List<Topic> topics = new ArrayList<>();
		List<String> list = readFile2List(path);
		for (String s : list) {
			String[] ts = s.split("\t");
			if (ts.length == 10) {
				Topic topic = new Topic(ts[0], ts[1], ts[2], ts[3], ts[4], ts[5], ts[6], ts[7], ts[8],
						Float.parseFloat(ts[9]));
				topics.add(topic);
			} else {
				String[] ss = new String[10];
				for (int i = 0; i < ts.length; i++) {
					ss[i] = ts[i];
				}
				Topic topic = new Topic(ss[0], ss[1], ss[2], ss[3], ss[4], ss[5], ss[6], ss[7], ss[8],
						ss[9] == null ? 101 : Float.parseFloat(ss[9]), ts.length);
				if (topic.getTopicList().length < 4) {
					System.out.println(topic.getTopicList());
				}
				topics.add(topic);
			}
		}
		return topics;
	}

	/**
	 * @Title: getTopic
	 * @Description: 读Topic
	 * @param path
	 * @return
	 * @return List<Topic>
	 * @author Alexis
	 */
	public List<Topic> getTopic(String database, String table) {
		List<Topic> topics = new ArrayList<>();
		Connection conn = ConnectionPool.getConnection(database, "localhost");
		Statement st = null;
		ResultSet rs = null;
		String sql = "SELECT IFNULL(tag1,''),IFNULL(tag2,''),IFNULL(tag3,''),IFNULL(tag4,''),IFNULL(topic1,''),IFNULL(topic2,''),IFNULL(topic3,''),IFNULL(topic4,''),IFNULL(topic5,''),IFNULL(score,101) from "
				+ database + "." + table;
		try {
			st = conn.createStatement();
			rs = st.executeQuery(sql);
			while (rs.next()) {
				String tag1 = rs.getString(1);
				String tag2 = rs.getString(2);
				String tag3 = rs.getString(3);
				String tag4 = rs.getString(4);
				String topic1 = rs.getString(5);
				String topic2 = rs.getString(6);
				String topic3 = rs.getString(7);
				String topic4 = rs.getString(8);
				String topic5 = rs.getString(9);
				float score = rs.getFloat(10);
				Topic topic = new Topic(tag1, tag2, tag3, tag4, topic1, topic2, topic3, topic4, topic5, score);
				topics.add(topic);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return topics;
	}

	/**
	 * @Title: getAvd
	 * @Description: 读副词
	 * @return
	 * @return Map<String,Float>
	 * @author Alexis
	 */
	public Map<String, Float> getAvd() {
		Connection conn = ConnectionPool.getConnection();
		Map<String, Float> map = new HashMap<>();
		Statement st = null;
		ResultSet rs = null;
		String sql = "select adv_word,adv_degree from carvoice_autohome.topic_adv ORDER BY CHAR_LENGTH(adv_word) DESC";
		try {
			st = conn.createStatement();
			rs = st.executeQuery(sql);
			while (rs.next()) {
				map.put(rs.getString(1), rs.getFloat(2));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return map;
	}

}
